package com.interfaz.tabla.jcb;

import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;

import javax.swing.AbstractCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.controlador.MarcaController;
import com.controlador.ModeloController;
import com.help.ModeloRenderer;
import com.modelo.Modelo;

public class JCModeloEditor extends AbstractCellEditor implements TableCellEditor{

	private JComboBox jcbMod;
	
	private Object valorActual;
	private Modelo modelo = null;
	private ModeloController modelo_controller;
	private Vector<Modelo> vector_modelo;
	
	
	public JCModeloEditor(){
		
		
		
		
		modelo_controller = new ClassPathXmlApplicationContext("applicationContext.xml").getBean("modeloService",ModeloController.class);
		vector_modelo = new Vector<Modelo>();
		vector_modelo = modelo_controller.llenarModelos();
		jcbMod = new JComboBox(vector_modelo);
		jcbMod.setRenderer(new ModeloRenderer());
		ItemListener itemL = new ItemListener(){
										public void itemStateChanged(ItemEvent ie){
											
											if(ie.getStateChange()==ItemEvent.SELECTED){
												//ItemSelectable itemS = ie.getItem();
												Modelo el_mod = (Modelo)ie.getItem();
												if(el_mod!=null){
													System.out.println("JCModeloEditor: "+el_mod.getNombre());
													valorActual = el_mod;
												
												}else{
													System.out.println("JCCategoriaEditor: "+el_mod.getNombre());

												}
											}
										}
							};
		jcbMod.addItemListener(itemL);
	}
	
	@Override
	public Object getCellEditorValue() {
		// TODO Auto-generated method stub
		return valorActual;
	}

	@Override
	public Component getTableCellEditorComponent(JTable arg0, Object arg1,
			boolean arg2, int arg3, int arg4) {
		// TODO Auto-generated method stub
		return jcbMod;
	}

}
